package com.cws4cloud.cws.mall.basic.biz.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cws4cloud.cws.common.core.util.R;
import com.cws4cloud.cws.common.log.annotation.SysLog;
import com.cws4cloud.cws.mall.basic.api.entity.Goods;
import com.cws4cloud.cws.mall.basic.api.service.GoodsService;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;


/**
 * 商品
 *
 * @author zhulj
 * @date 2021-08-15 22:39:14
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/goods")
@Api(value = "goods", tags = "商品管理")
public class GoodsController {

    private final  GoodsService goodsService;

    /**
     * 分页查询
     * @param page 分页对象
     * @param goods 商品
     * @return
     */
    @ApiOperation(value = "分页查询", notes = "分页查询")
    @GetMapping("/page")
    @PreAuthorize("@pms.hasPermission('basic_goods_get')" )
    public R getGoodsPage(Page page, Goods goods) {
        return R.ok(goodsService.page(page, Wrappers.query(goods)));
    }


    /**
     * 通过id查询商品
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @GetMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_goods_get')" )
    public R getById(@PathVariable("id" ) Integer id) {
        return R.ok(goodsService.getById(id));
    }

    /**
     * 新增商品
     * @param goods 商品
     * @return R
     */
    @ApiOperation(value = "新增商品", notes = "新增商品")
    @SysLog("新增商品")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('basic_goods_add')" )
    public R save(@RequestBody Goods goods) {
        return R.ok(goodsService.save(goods));
    }

    /**
     * 修改商品
     * @param goods 商品
     * @return R
     */
    @ApiOperation(value = "修改商品", notes = "修改商品")
    @SysLog("修改商品")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('basic_goods_edit')" )
    public R updateById(@RequestBody Goods goods) {
        return R.ok(goodsService.updateById(goods));
    }

    /**
     * 通过id删除商品
     * @param id id
     * @return R
     */
    @ApiOperation(value = "通过id删除商品", notes = "通过id删除商品")
    @SysLog("通过id删除商品")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('basic_goods_del')" )
    public R removeById(@PathVariable Integer id) {
        return R.ok(goodsService.removeById(id));
    }

}
